<template>
  <view class="page-all">
    <Header :leftIconSize="20" bgColor="#fff" title="创建检查"></Header>
    <view class="radius-contain">
      <uv-form :model="form" labelPosition="left">
        <uv-form-item borderBottom prop="schoolName" @click="toSelectSchool"  v-if="entryType!='6'">
          <template #label>
            <div class="requireStar">选择学校</div>
          </template>
          <uv-input v-model="form.schoolName" border="none" disabled="true" disabledColor="#fff" inputAlign="right"
                    placeholder="请选择检查学校" placeholderClass="placeholder-color"></uv-input>
          <template v-slot:right>
            <uv-icon name="arrow-right"></uv-icon>
          </template>
        </uv-form-item>

        <uv-form-item borderBottom prop="schoolName" @click="toSelectSchool"  v-if="entryType=='6'">
          <template #label>
            <div class="requireStar">选择配餐单位</div>
          </template>
          <uv-input v-model="form.schoolName" border="none" disabled="true" disabledColor="#fff" inputAlign="right"
                    placeholder="请选择检查配餐单位" placeholderClass="placeholder-color"></uv-input>
          <template v-slot:right>
            <uv-icon name="arrow-right"></uv-icon>
          </template>
        </uv-form-item>


        <uv-form-item borderBottom prop="canteenName" @click="showCanteenPicker" v-if="entryType=='1'">
          <template #label>
            <div class="requireStar">选择食堂</div>
          </template>
          <uv-picker ref="canteenPicker" :columns="[canteenColumns]" keyName="name"
                     @confirm="canteenConfirm"></uv-picker>
          <uv-input v-model="form.canteenName" border="none" disabled="true" disabledColor="#fff" inputAlign="right"
                    placeholder="请选择检查食堂" placeholderClass="placeholder-color"></uv-input>
          <template v-slot:right>
            <uv-icon name="arrow-right"></uv-icon>
          </template>
        </uv-form-item>
        <uv-form-item borderBottom label="" prop="targetName" @click="toSelectIndex">
          <template #label>
            <div class="requireStar">选择指标</div>
          </template>
          <uv-input v-model="form.targetName" border="none" disabled="true" disabledColor="#fff" inputAlign="right"
                    placeholder="请选择检查指标" placeholderClass="placeholder-color"></uv-input>
          <template v-slot:right>
            <uv-icon name="arrow-right"></uv-icon>
          </template>
        </uv-form-item>
      </uv-form>
    </view>
    <view class="check-bottom-box" @click.stop>
      <button class="sure-button" @click.stop="submit()">确认</button>
    </view>
  </view>
</template>

<script setup>
import {getCurrentInstance, reactive, ref} from 'vue'
import {onShow} from '@dcloudio/uni-app'
import publicAPI from "@/api/law/public/public";
const {proxy} = getCurrentInstance();
let form = reactive({})

let canteenPicker = ref()
//食堂
let canteenColumns = ref([])

let entryType = ref(uni.getStorageSync('entryType'));
const schoolFlag = ref(0)
onShow(() => {
  //接收选择学校传回的数据 data:{id,name}
  uni.$on('schoolData', function (data) {
    if(form.schoolId != data[0].id){
      form.canteenId='';
      form.canteenName='';
    }
    form.schoolName = data[0].name
    form.schoolId = data[0].id;
    if(entryType.value!='6') {
      getCanteenList();
    }
  })
  //接收选择指标传回的数据  data:{id,name}
  uni.$on('indexData', function (data) {
    form.targetName = data[0].name
    form.targetId = data[0].id
  })
})

//跳转到选择学校
function toSelectSchool() {
  if(entryType.value=='6'){
    uni.navigateTo({
      url: `/packageLawEnforcement/pages/createCheck/orgCater?radiovalue=${form.schoolId}`
    })
  }else{
    if(entryType.value=='1'){
      schoolFlag.value = 1;
    }
    uni.navigateTo({
      url: `/packageLawEnforcement/pages/createCheck/selectSchool?radiovalue=${form.schoolId}&schoolFlag=${schoolFlag.value}`
    })
  }
}

//获取学校列表
let getCanteenList = async () => {
  let res = await proxy.http.asyncPost(publicAPI.getCanteenNoTenantId, {schoolId: form.schoolId});
  if (res.code === 0) {
    console.log("获取食堂列表")
    canteenColumns.value = [...res.data];
    console.log(canteenColumns.value)
  } else {
    await uni.showToast({title: res.msg, icon: 'none'});
  }
};

//选择食堂
function showCanteenPicker() {
  canteenPicker.value.open()
}

//跳转选择指标
function toSelectIndex() {
  uni.navigateTo({
    url: `/packageLawEnforcement/pages/createCheck/selectIndex?radiovalue=${form.targetId}`
  })
}

//选择食堂确认
function canteenConfirm(e) {
  form.canteenId = e.value[0].id
  form.canteenName = e.value[0].name

}

function submit() {
  if(entryType.value=='1'){
    if (!form.schoolId) {
      uni.showToast({
        icon: 'error',
        title: '请先选择学校！'
      })
      return;
    }
    if (!form.canteenId) {
      uni.showToast({
        icon: 'error',
        title: '请选择食堂！'
      })
      return;
    }
  }
  if(entryType.value!='6'){
    if (!form.schoolName) {
      uni.showToast({
        icon: 'error',
        title: '请选择检查学校！'
      })
      return;
    }
  }else{
    if (!form.schoolName) {
      uni.showToast({
        icon: 'error',
        title: '请选择检查配餐单位！'
      })
      return;
    }
  }
  if (!form.targetName) {
    uni.showToast({
      icon: 'error',
      title: '请选择检查指标！'
    })
    return;
  }
  uni.navigateTo({
    url: `/packageLawEnforcement/pages/startCheck/index?data=${encodeURIComponent(JSON.stringify(form))}`
  })
}
</script>

<style lang="scss" scoped>
::v-deep .placeholder-color {
  color: rgba(0, 0, 0, 0.3);
  font-size: 32rpx;
}

::v-deep .requireStar {
  width: 30%;
}

::v-deep .requireStar:after {
  vertical-align: bottom; //加上星号会上下居中，不加会在上角展示
  content: ' *';
  color: rgba(255, 59, 48, 1);
  font-size: 100%;
}
.requireStar {
   width: 35%!important;
 }
</style>
